package com.polaris.live.popup

import android.content.Context
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ScreenUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.adapter.GamePlayWinAdapter
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.AppCodeUtils
import com.polaris.live.databinding.PopupGamePlayWinBinding
import com.polaris.live.resp.back_resp.GameRankResp

/**
 * GamePlayWinPopup 赢钱数据
 * @author Created by 天晴 on 2024/1/19 11:16
 * @since 1.0.0
 **/
class GamePlayWinPopup(
    context: Context,
    private val gameRankResp: List<GameRankResp>,
    private val back: () -> Unit
) : CenterPopupView(context) {

    private lateinit var mBinding: PopupGamePlayWinBinding

    override fun getImplLayoutId(): Int {
        return R.layout.popup_game_play_win
    }

    val mAdapter by lazy { GamePlayWinAdapter() }

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupGamePlayWinBinding.bind(popupImplView)
        if (AppCodeUtils.isAR()) {
            val lp = mBinding.recyclerView.layoutParams as ConstraintLayout.LayoutParams
            lp.endToEnd =  ConstraintLayout.LayoutParams.UNSET
            lp.startToStart = ConstraintLayout.LayoutParams.PARENT_ID
            lp.marginStart = 30.autoDp
            mBinding.recyclerView.layoutParams = lp
        }
        mBinding.tvAnswerPhone.setOnSingleClickListener {
            dismiss()
            back.invoke()
        }
        mBinding.close.setOnSingleClickListener {
            dismiss()
        }
        mBinding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            adapter = mAdapter
        }
        mAdapter.data.addAll(gameRankResp)
        mAdapter.notifyDataSetChanged()
    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    override fun getPopupWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    companion object {

        fun show(context: Context, gameRankResp: List<GameRankResp>, back: () -> Unit) {
            //存弹窗时间
            CacheUtil.setLastGameWinPopTime(CacheUtil.getServerTime())
            XPopup.Builder(context)
                .hasNavigationBar(false)
                .isDestroyOnDismiss(true)
                .asCustom(GamePlayWinPopup(context, gameRankResp, back))
                .show()
        }
    }
}